package com.xiaofeng.pochi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaofeng.pochi.common.Page;
import com.xiaofeng.pochi.common.Result;
import com.xiaofeng.pochi.pojo.LoginUser;
import com.xiaofeng.pochi.pojo.SysBanner;
import com.xiaofeng.pochi.pojo.SysUser;
import com.xiaofeng.pochi.pojo.vo.SysUserVo;
import com.xiaofeng.pochi.service.SysBannerService;
import com.xiaofeng.pochi.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/08/05/23:04
 * @Description:
 */
@RestController
@RequestMapping("/sysBanner")
public class SysBannerController {

    @Autowired
    private SysBannerService sysBannerService;

    /**
     * 添加
     *
     * @param sysBanner
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<?> save(@RequestBody SysBanner sysBanner) {
        LoginUser loginUser = ShiroUtils.getLoginUser();
        sysBanner.setCreateBy(loginUser.getUsername());
        sysBanner.setUpdateBy(loginUser.getUsername());
        sysBannerService.save(sysBanner);
        return new Result<>("添加成功");
    }

    /**
     * 修改
     *
     * @param sysBanner
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result<?> update(@RequestBody SysBanner sysBanner) {
        sysBannerService.update(sysBanner);
        return new Result<>("修改成功");
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public Result<SysBanner> get(@PathVariable Long id) {
        SysBanner sysBanner = sysBannerService.get(id);
        return new Result<>(sysBanner);
    }

    /**
     * 根据id删除
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result<?> delete(@PathVariable Long id) {
        QueryWrapper<SysBanner> query = new QueryWrapper<>();
        query.lambda().eq(SysBanner::getId,id);
        SysBanner sysBanner = new SysBanner();
        sysBanner.setDeleted(1);
        boolean b = sysBannerService.update(sysBanner,query);
        if (b){
            return new Result<>("修改成功");
        }
        return new Result<>("修改失败");
    }

    /**
     * 根据id启用
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/enable/{id}", method = RequestMethod.PUT)
    public Result<?> enable(@PathVariable Long id) {
        sysBannerService.enable(id);
        return new Result<>("启用成功");
    }

    /**
     * 根据id弃用
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/disable/{id}", method = RequestMethod.PUT)
    public Result<?> disable(@PathVariable Long id) {
        sysBannerService.disable(id);
        return new Result<>("弃用成功");
    }

    /**
     * 分页查询
     * @param page
     * @return
     */
    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    public Result<Page<SysBanner>> getByPage(@RequestBody Page<SysBanner> page) {
        return new Result<>(sysBannerService.getByPage(page));
    }

    /**
     * 首页查询轮播图
     * @return
     */
    @RequestMapping(value = "/getBannerList", method = RequestMethod.GET)
    public Result<List<SysBanner>> getBannerList() {
        List<SysBanner> list = sysBannerService.getBannerList();
        return new Result<>(list);
    }

    /**
     * 点击量+1
     * @return
     */
    @RequestMapping(value = "/addClickCount/{id}", method = RequestMethod.PUT)
    public Result<?> addClickCount(@PathVariable Long id) {
        sysBannerService.addClickCount(id);
        return new Result<>();
    }
}
